import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_trip_demo/model/common_model.dart';
import 'package:flutter_trip_demo/utils/navigator/navigator_util.dart';
import '../model/gridview_navigator_model.dart';
import 'my_webview.dart';

/*
* 1.网格布局
* */
class GridNav extends StatelessWidget {
  final GridNavModel? gridNavModel;

  const GridNav({Key? key, required this.gridNavModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PhysicalModel(
      color: Colors.transparent,
      borderRadius: BorderRadius.circular(6),
      clipBehavior: Clip.antiAlias,
      child: Column(
        children: _gridNavItems(context),
      ),
    );
  }

  //在这个地方进行搭建
  _gridNavItems(BuildContext context) {
    List<Widget> items = [];
    if (gridNavModel == null) {
      return items;
    }
    if (gridNavModel!.hotel != null) {
      items.add(_gridNavItem(context, gridNavModel!.hotel, true));
    }
    if (gridNavModel!.flight != null) {
      items.add(_gridNavItem(context, gridNavModel!.flight, false));
    }
    if (gridNavModel!.travel != null) {
      items.add(_gridNavItem(context, gridNavModel!.travel, false));
    }
    return items;
  }

  _gridNavItem(BuildContext context, GridNavItem gridNavItem, bool first) {
    List<Widget> items = [];
    items.add(_mainItem(context, gridNavItem.mainItem));
    items.add(_doubleItem(context, gridNavItem.item1, gridNavItem.item2));
    items.add(_doubleItem(context, gridNavItem.item3, gridNavItem.item4));
    List<Widget> exanditems = [];
    items.forEach((item) {
      exanditems.add(Expanded(
        child: item,
        flex: 1,
      ));
    });
    Color startcolor = Color(int.parse("0xff" + gridNavItem.startColor));
    Color endcolor = Color(int.parse("0xff" + gridNavItem.endColor));
    return Container(
      height: 88,
      margin: first ? null : const EdgeInsets.only(top: 3),
      decoration: BoxDecoration(
          //  渐变色
          gradient: LinearGradient(colors: [startcolor, endcolor])),
      child: Row(children: exanditems),
    );
  }

  _mainItem(BuildContext context, CommonModel commonModel) {
    return _warpGesture(
        context,
        Stack(
          alignment: AlignmentDirectional.topCenter,
          children: <Widget>[
            Image.network(
              commonModel.icon,
              fit: BoxFit.contain,
              height: 88,
              width: 121,
              alignment: AlignmentDirectional.bottomEnd,
            ),Container(
              margin: const EdgeInsets.only(top: 10),
               child:  Text(
                 commonModel.title,
                 style: const TextStyle(fontSize: 14, color: Colors.white),
               ),
            ),
          ],
        ),
        commonModel);
  }

  _doubleItem(
      BuildContext context, CommonModel topItem, CommonModel bottomItem) {
    return Column(
      children: <Widget>[
        Expanded(child: _item(context, topItem, true)),
        Expanded(child: _item(context, bottomItem, false)),
      ],
    );
  }

  _item(BuildContext context, CommonModel item, bool first) {
    BorderSide borderSide = const BorderSide(width: 0.8, color: Colors.white);
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
          decoration: BoxDecoration(
              border: Border(
            left: borderSide,
            bottom: first ? borderSide : BorderSide.none,
          )),
          child: _warpGesture(
              context,
              Center(
                child: Text(
                  item.title,
                  textAlign: TextAlign.center,
                  style: const TextStyle(fontSize: 14, color: Colors.white),
                ),
              ),
              item)),
    );
  }

  //实现一个他的跳转
  _warpGesture(BuildContext context, Widget widget, CommonModel commonModel) {
    return GestureDetector(
      onTap: () {
        NavigatorUtil.push(
          context,
          MyWebView(
            url: commonModel.url,
            statusBarColor: commonModel.statusBarColor,
            hideAppBar: commonModel.hideAppBar,
          )
        );
      },
      child: widget,
    );
  }
}
//多个backForbid  我写的时候
